<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@include file="islogin.jsp"%>
<%@page import="com.ch7.dal.Class"%>
<%@page import="com.ch7.model.*"%>
<%@page import="com.ch7.common.DataConverter"%>
<%@page import="com.ch7.dal.Blog"%>
<%@page import="net.fckeditor.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>后台博文编辑与添加页</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<%
	request.setCharacterEncoding("utf-8");
	String action = request.getParameter("action");
	String pageAction = "";
	Integer id = DataConverter.toInt(request.getParameter("id"));
	System.out.println(DataConverter.toInt(request.getParameter("id")));
	BlogInfo info = new BlogInfo();
	Blog blog = new Blog();
	if ("update".equals(action) || "insert".equals(action)) {
		if ("update".equals(action)) {
			info = blog.getBlogInfo(id);
			if (info == null) {
				out.println("<script>alert('博文ID有误')；window.location.href='login.jsp';</script>");
			}
			info.setId(id);
		}
		info.setTitle(request.getParameter("textTitle"));
		info.setClassid(DataConverter.toInt(request.getParameter("selClass")));
		info.setContext(request.getParameter("content"));
		if ("insert".equals(action)) {
			info.setCreatedtime(Utility.getNowDateTime());
			blog.insert(info);
		} else 
			blog.update(info);
			response.sendRedirect("blog-manage.jsp");
		}
		if ("edit".equals(action) || "add".equals(action)) {
			info = blog.getBlogInfo(id);
			if (info == null) {

			}
			pageAction = "update";
		}

		if ("add".equals(action)) {
			pageAction = "insert";
		}
		
	

FCKeditor fckEditor= new FCKeditor(request,"content");
		fckEditor.setHeight("400");
		fckEditor.setValue(info.getContext());
	Class cls = new Class();
	List<ClassInfo> list = cls.getList();
%>

<script>
function FCKeditor_OnComplete(editorInstance){
window.status=editorInstance.Description;
}
</script>
</head>

<body>
	<p>当前位置：博文编辑/添加</p>
	<form id="form1" name="form1" method="post" action="manager/blog-edit.jsp">
		<table width="100%" border="1">
			<tr>
				<td>博文标题</td>
				<td><input type="text" name="textTitle" id="textTitle"
					width=" 500px" value=" <%=info.getTitle()%>" /></td>
			</tr>
			
			<tr>
				<td>博文所属分类</td>
				<td><select name="selClass" id="selClass">
						<%
							for (ClassInfo cinfo : list) {
						%>
						<option value="<%=cinfo.getId()%>"
							<%if (cinfo.getId() == info.getClassid())
					out.print("selected");%>>
                            <%=cinfo.getName()%></option>
						<%}%>
				</select></td>
			</tr>
			<tr>
			<td>博文内容</td>
			<td><%=fckEditor %></td>
			</tr>
			<tr>
			<td colspan="2">
			<input type="submit" name="button" id="button" value="提交"/>
			<input type="reset" name="button2" id="button2" value="重置"/>
			<input type="hidden" name="action" value="<%=pageAction%>"/>
			<input type="hidden" name="id" value="<%=info.getId()%>"/>
			</td>
			</tr>
		</table>
	</form>
	<br>
</body>
</html>
